# -*- coding: utf-8 -*-
import time
import sys
from PyQt4 import QtGui,QtCore
from PyQt4.QtCore import *
from collections import OrderedDict
from eventEngine import *
from httpservice import *
from dockwidget import *








class MarketDataMonitorForm(DockWidget):
    """用于显示市场行情"""
    signal = pyqtSignal(type(Event()))
    # ----------------------------------------------------------------------
    def __init__(self ,eventEngine,windowTitle  , parent=None,):
        """Constructor"""
        super(MarketDataMonitorForm, self).__init__(parent,windowTitle)
        self.__eventEngine = eventEngine
        self.setAllowedAreas(Qt.LeftDockWidgetArea)
        self.setFeatures(QtGui.QDockWidget.DockWidgetClosable)
        self.dictData = {}
        self.dictLabels = OrderedDict()
        self.dictLabels['code'] = u'品种代码'
        self.dictLabels['name'] = u'交易品种'
        self.dictLabels['buy'] = u'买价'
        self.dictLabels['sell'] = u'卖价'
        self.initUi()
        self.registerEvent()

    def initUi(self):

        self.tabWidget = QtGui.QTabWidget(self)
        self.tabWidget.setTabPosition(QtGui.QTabWidget.South)
        self.marketTable = QtGui.QTableWidget(self)
        self.marketTable.setColumnCount(len(self.dictLabels))
        self.marketTable.setHorizontalHeaderLabels(self.dictLabels.values())
        self.marketTable.verticalHeader().setVisible(False)  # 关闭左边的垂直表头
        self.marketTable.setEditTriggers(QtGui.QTableWidget.NoEditTriggers)  # 设为不可编辑状态
        self.marketTable.setSelectionBehavior(QtGui.QTableWidget.SelectRows)#行选
        self.marketTable.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)#只能单选
        self.marketTable.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)
        self.marketTable.resizeRowsToContents()
        self.tabWidget.addTab(self.marketTable, u'交易品种')

        self.addWidget(self.tabWidget)

    def registerEvent(self):
        self.signal.connect(self.updateData)
        self.__eventEngine.register(EVENT_MARKETDATA, self.signal.emit)

    def updateData(self, event):
        data = event.dict_['data']
        for k,v in  data.viewitems():
            if k  in self.dictData:
                d = self.dictData[k]
                for label, cell in d.items():
                    cell.setText(v[label])
            else:
                row = self.marketTable.rowCount()
                self.marketTable.insertRow(row)
                d = {}
                for col, label in enumerate(self.dictLabels.keys()):
                    cell = QtGui.QTableWidgetItem(v[label])
                    #设置颜色
                    if v['open'] > v['preClose'] :
                       cell.setTextColor(QtCore.Qt.red)
                    else:
                       cell.setTextColor(QtCore.Qt.green)
                    if label == 'name'or label == 'code':
                        cell.setTextColor(QtCore.Qt.blue)
                    cell.setTextAlignment(Qt.AlignHCenter|Qt.AlignVCenter)#垂直居中
                    self.marketTable.setItem(row, col, cell)
                    d[label] = cell
            self.dictData[k] = d


